package com.niit.chat.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.niit.chat.entity.User;
import com.niit.chat.mapper.UserMapper;
import com.niit.chat.service.UserService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.Optional;
import java.util.function.Function;

/**
 * 用户服务实现类
 * 实现用户相关的业务逻辑
 */
@Service
public class UserServiceImpl implements UserService {

    private final UserMapper userMapper;

    public UserServiceImpl(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    @Override
    public boolean saveBatch(Collection<User> entityList, int batchSize) {
        return false; // 实现 saveBatch 方法，目前返回 false
    }

    @Override
    public boolean saveOrUpdateBatch(Collection<User> entityList, int batchSize) {
        return false; // 实现 saveOrUpdateBatch 方法，目前返回 false
    }

    @Override
    public boolean updateBatchById(Collection<User> entityList, int batchSize) {
        return false; // 实现 updateBatchById 方法，目前返回 false
    }

    @Override
    public boolean saveOrUpdate(User user) {
        return false; // 实现 saveOrUpdate 方法，目前返回 false
    }

    @Override
    public User getOne(Wrapper<User> queryWrapper, boolean throwEx) {
        return userMapper.selectOne(queryWrapper);
    }

    @Override
    public Optional<User> getOneOpt(Wrapper<User> queryWrapper, boolean throwEx) {
        return Optional.ofNullable(userMapper.selectOne(queryWrapper));
    }

    @Override
    public java.util.Map<String, Object> getMap(Wrapper<User> queryWrapper) {
        return null;
    }

    @Override
    public <V> V getObj(Wrapper<User> queryWrapper, Function<? super Object, V> mapper) {
        return null;
    }

    @Override
    public BaseMapper<User> getBaseMapper() {
        return userMapper; // 返回 userMapper 作为 BaseMapper
    }

    @Override
    public Class<User> getEntityClass() {
        return null;
    }

    // 实现 UserService 接口的所有方法
    @Override
    public Optional<String> getNicknameById(Long userId) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getId, userId).select(User::getNickname);
        User user = userMapper.selectOne(wrapper);
        return Optional.ofNullable(user != null ? user.getNickname() : null);
    }
}